package com.cershy.chatroomserver.controller;

import com.cershy.chatroomserver.annotation.UrlLimit;
import com.cershy.chatroomserver.annotation.Userid;
import com.cershy.chatroomserver.entity.ChatList;
import com.cershy.chatroomserver.entity.Group;
import com.cershy.chatroomserver.service.ChatListService;
import com.cershy.chatroomserver.utils.ResultUtil;
import com.cershy.chatroomserver.vo.chatList.CreateVo;
import com.cershy.chatroomserver.vo.chatList.DeleteVo;
import com.cershy.chatroomserver.vo.chatList.GroupLatestMessageVo;
import com.cershy.chatroomserver.vo.chatList.ReadVo;
import com.cershy.chatroomserver.vo.message.UnreadStatusVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/v1/chat-list")
public class ChatListController {

    @Resource
    ChatListService chatListService;

    @UrlLimit
    @GetMapping("/list/private")
    public Object privateList(@Userid String userId) {
        List<ChatList> result = chatListService.privateList(userId);
        return ResultUtil.Succeed(result);
    }
    @UrlLimit
    @GetMapping("/grouplist")
    public Object groupList(@Userid String userId) {
        List<Group> result = chatListService.getGroupList(userId);
        return ResultUtil.Succeed(result);
    }
    @UrlLimit
    @GetMapping("/groupall")
    public Object groupAll() {
        List<Group> result = chatListService.getAllGroups();
        return ResultUtil.Succeed(result);
    }


    @UrlLimit
    @GetMapping("/group/latestmessage")
    public Object getGroupLatestMessages(@Userid String userId) {
        List<GroupLatestMessageVo> result = chatListService.getGroupLatestMessages(userId);
        return ResultUtil.Succeed(result);
    }

    @UrlLimit
    @GetMapping("/read-status")
    public Object getUnreadMessageCount(@Userid String userId, @RequestParam("targetId") String targetId) {
        int unreadCount = chatListService.getUnreadCount(userId, targetId);
        return ResultUtil.Succeed(new UnreadStatusVo(targetId, unreadCount));
    }


    @UrlLimit
    @PostMapping("/create")
    public Object create(@Userid String userId, @RequestBody @Valid CreateVo createVo) {
        ChatList result = chatListService.create(userId, createVo.getTargetId());
        return ResultUtil.Succeed(result);
    }

    @UrlLimit
    @PostMapping("/read")
    public Object read(@Userid String userId, @RequestBody @Valid ReadVo readVo) {
        boolean result = chatListService.read(userId, readVo.getTargetId());
        return ResultUtil.Succeed(result);
    }

    @UrlLimit
    @PostMapping("/delete")
    public Object delete(@Userid String userId, @RequestBody @Valid DeleteVo deleteVo) {
        boolean result = chatListService.delete(userId, deleteVo.getChatListId());
        return ResultUtil.Succeed(result);
    }
}
